package lg.minecraft.plugin.worldprotect.listener;

import lg.minecraft.plugin.worldprotect.LGWorldProtect;
import lg.minecraft.plugin.worldprotect.WorldSettingType;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.*;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerEvent implements Listener {

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerBucketFill(PlayerBucketFillEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.PLACE)) {
            if (LGWorldProtect.getMainConfig().isEnableTip()) player.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.PLACE));
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (worldSettings.isEnabled(WorldSettingType.DESTRUCTION)) {
            if (LGWorldProtect.getMainConfig().isEnableTip()) player.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.DESTRUCTION));
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();
        Action action = event.getAction();
        Block block = event.getClickedBlock();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);

        if (action == Action.PHYSICAL && block != null) {
            if (worldSettings.isEnabled(WorldSettingType.TRAMPLE)) {
                Material type = block.getType();
                if (type == Material.FARMLAND) event.setCancelled(true);
            }
        } else if (action == Action.RIGHT_CLICK_BLOCK && block != null) {
            Material type = block.getType();
            if (worldSettings.isEnabled(WorldSettingType.CONTAINER)) {
                for (Material container : LGWorldProtect.getMainConfig().getContainers()) {
                    if (type == container) {
                        event.setCancelled(true);
                        break;
                    }
                }
            }
            if (worldSettings.isEnabled(WorldSettingType.ANVIL) && (type == Material.ANVIL || type == Material.CHIPPED_ANVIL || type == Material.DAMAGED_ANVIL)) event.setCancelled(true);
            if (worldSettings.isEnabled(WorldSettingType.TABLE) && type == Material.CRAFTING_TABLE) event.setCancelled(true);
            if (worldSettings.isEnabled(WorldSettingType.BEACON) && type == Material.BEACON) event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerToggleFlightEvent(PlayerToggleFlightEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        GameMode gameMode = player.getGameMode();
        World world = player.getWorld();

        if (player.isOp() || gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.FLY)) {
            event.setCancelled(true);
        }

    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
        Player player = event.getPlayer();
        GameMode gameMode = player.getGameMode();
        World world = player.getWorld();

        if (player.isOp() || gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.FLY)) {
            if (player.isFlying()) player.setFlying(false);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        GameMode gameMode = player.getGameMode();
        World world = player.getWorld();

        if (player.isOp() || gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.FLY)) {
            if (player.isFlying()) player.setFlying(false);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();

        if (player.isOp()) return;

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.ITEM_DROP)) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerTeleport(PlayerTeleportEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        World world = player.getWorld();
        PlayerTeleportEvent.TeleportCause cause = event.getCause();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.JOIN_WORLD)) {
            Location from = event.getFrom();
            Location to = event.getTo();
            if (to == null || from.getWorld() == to.getWorld()) {
                return;
            }
            World to_world = to.getWorld();
            if (to_world != null && player.hasPermission("WorldProtect.JoinWorld." + to_world.getName())) {
                return;
            }
            if (LGWorldProtect.getMainConfig().isEnableTip()) player.sendMessage(LGWorldProtect.getLanguage().getWorldSettingTip(WorldSettingType.JOIN_WORLD));
            event.setCancelled(true);
            return;
        }

        if (player.isOp()) return;

        if (worldSettings.isEnabled(WorldSettingType.ENDER_PEARL) && cause == PlayerTeleportEvent.TeleportCause.ENDER_PEARL) {
            event.setCancelled(true);
        } else if (worldSettings.isEnabled(WorldSettingType.CHORUS_TP) && cause == PlayerTeleportEvent.TeleportCause.CHORUS_FRUIT) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onPlayerTeleport(PlayerDeathEvent event) {
        Player player = event.getEntity();
        World world = player.getWorld();

        WorldSettings worldSettings = LGWorldProtect.getWorldSettings(world);
        if (worldSettings.isEnabled(WorldSettingType.KEEP_EXP)) {
            event.setKeepLevel(true);
        }
        if (worldSettings.isEnabled(WorldSettingType.KEEP_INV)) {
            event.setKeepInventory(true);
        }
    }

}
